<template>
  <div>
    <div style="display: flex; justify-content: space-between">
      <!-- 面包屑 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }"
          >首页</el-breadcrumb-item
        >
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 添加按钮 -->
      <el-button
        icon="el-icon-plus"
        circle
        type="primary"
        @click="addheros"
      ></el-button>
    </div>
    <!-- 数据 -->
    <el-table :data="list" style="width: 100%; margin-top: 20px">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品名称">
              
              <span style="margin-left: 10px">{{
                props.row.username
              }}</span>
            </el-form-item>
            <el-form-item label="商品价格">
              <span style="margin-left: 10px">{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="商品版本">
              <span style="margin-left: 10px">{{ props.row.version }}</span>
            </el-form-item>
            <el-form-item label="商品颜色">
              <span style="margin-left: 10px">{{ props.row.color }}</span>
            </el-form-item>
            <el-form-item label="商品简介">
              <span style="margin-left: 10px">{{ props.row.describe }}</span>
            </el-form-item>
            
            <el-form-item label=" ">
              <span class="sp1"><img :src="props.row.img" /></span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品 ID" prop="id"> </el-table-column>
      <el-table-column label="商品名称" prop="username"> </el-table-column>
      <el-table-column label="商品售价" prop="price"> </el-table-column>
      <el-table-column label="商品版本" prop="version"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            circle
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
          ></el-button>
          <el-popconfirm
            style="margin-left: 20px"
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="handleDelete(scope.$index, scope.row)"
            @cancel="cancel"
          >
            <el-button
              size="mini"
              type="danger"
              circle
              slot="reference"
              icon="el-icon-delete"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      direction="ltr"
      :before-close="handleClose"
    >
      <el-input
        v-model="edit.id"
        placeholder="商品ID"
        :disabled="idStatus"
      ></el-input>
      <el-input v-model="edit.username" placeholder="商品名称"></el-input>
      <el-input v-model="edit.price" placeholder="商品价格"></el-input>
      <el-input
        v-model="edit.img"
        placeholder="商品图片"
      ></el-input>
      <el-input v-model="edit.version" placeholder="商品版本"></el-input>
      <el-input v-model="edit.color" placeholder="商品颜色"></el-input>
      <el-input
        v-model="edit.describe"
        placeholder="商品简介"
      ></el-input>   
      <div style="margin-top: 20px; display: flex; justify-content: center">
        <el-button @click="cancel">取消</el-button>
        <el-button type="success" @click="handleSave">提交</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import url from '@/config/url'
export default {
  data() {
    return {
      list: [],
      drawer: false,
      idStatus: true,
      title: '修改商品信息',
      edit: {
        id: '',
        username: '',
        price: '',
        img: '',
        version:'',
        color:'',
        describe:''
      }
    }
  },
  created() {
    this.$http.get(url.heros).then((res) => {
      // console.log(res)
      this.list = res.data
    })
  },
  methods: {
    //添加商品
    addheros() {
      this.edit = {}
      this.drawer = true
      this.title = '添加商品信息'
      // 修改id为可编辑状态
      this.idStatus = false
    },
    //修改
    handleEdit(index, row) {
      this.drawer = true
      this.edit = row
      this.idStatus = true
    },
    //删除
    handleDelete(index, row) {
      console.log(row)
      this.$http.delete(url.heros + '/' + row.id).then((res) => {
        // console.log(row)
        // console.log(res)
        this.list.splice(index, 1)
      })
    },
    //一个组件实现两个需求
    handleSave() {
      //不能够编辑id 是修改方法
      if (this.idStatus) {
        this.$http
          .put(url.heros + '/' + this.edit.id, this.edit)
          .then((res) => {
            if (res.data) {
              this.$tip('修改成功')
              this.drawer = false
            } else {
              this.$tip('修改失败', 'error')
              // console.log(res.data)
            }
          })
      } else {
        this.$http.post(url.heros, this.edit).then((res) => {
          if (res.data) {
            this.$tip('添加成功')
            this.drawer = false
            window.location.reload()
          } else {
            this.$tip('添加失败', 'error')
          }
        })
      }
    },
    cancel() {
      this.drawer = false
    },
    //关闭
    handleClose(done) {
      done()
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-table-expand {
  font-size: 0;
}
:deep(.demo-table-expand label) {
  width: 90px;
  color: #4c91f1;
  margin-left: 10px;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}

.sp1 {
  position: absolute;
  top: -190px;
  left: 340px;
  img {
    width: 150px;
    height: 150px;
    position: relative;
  }
}
.el-table__expanded-cell {
  margin-left: 50px !important;
}
:deep(.el-input__inner) {
  width: 90%;
  margin-left: 10px;
  margin-top: 10px;
}
</style>